package com.wmjsoft.plugins.nutzplugin.dao

import com.wmjsoft.plugins.nutzplugin.utils.illegalValidation
import org.nutz.dao.entity.annotation.Table
import java.lang.Exception

/**
 * 如果我没有写注释，请提醒我
 * @program: Maternal
 * @description:
 * @author: Mr.Lee
 * @create: 2021-02-20 15:26
 */
class Ere {

    var filed: String? = null

    var op: String? = null

    var value: Any? = null

    constructor(filed: String, op: String, value: Any?,vararg clazzs: Class<*>?) {
        illegalValidation(filed,op)
        if(value is String){
            illegalValidation(value)
        }
        this.filed = addTableName(filed,*clazzs)
        this.op = op
        this.value = value
    }



    private fun addTableName(filed: String, vararg clazzs: Class<*>?): String {
        return if (clazzs.isEmpty()) {
            filed
        } else {
            getTableName(*clazzs) + "." + filed
        }
    }


    private fun getTableName(vararg clazzs: Class<*>?): String {
        return try {
            val o = clazzs[0]!!.newInstance()
            val table = o.javaClass.getAnnotation(Table::class.java)
            return table.value
        } catch (e: Exception) {
            throw RuntimeException("获取表名发生异常，请检查注解是否添加")
        }
    }
}